package com.xbongbong.admin.service.impl;

import com.xbongbong.admin.domain.entity.RaidersEntity;
import com.xbongbong.admin.domain.entity.RaidersGroupEntity;
import com.xbongbong.admin.help.RaidersHelp;
import com.xbongbong.admin.model.RaidersCorpModel;
import com.xbongbong.admin.model.RaidersGroupModel;
import com.xbongbong.admin.pojo.RaidersNormalPojo;
import com.xbongbong.admin.pojo.dto.RaidersGroupListDTO;
import com.xbongbong.admin.pojo.vo.RaidersGroupListVO;
import com.xbongbong.admin.service.RaidersGroupService;
import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.enums.DelEnum;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.UserVO;
import com.xbongbong.pro.constant.RaidersConstant;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * 攻略分组
 *
 * @author 徐俊杰
 * @version v1.0
 * @date 2019/9/27 14:54
 * @since v1.0
 */
@Service("raidersGroupService")
public class RaidersGroupServiceImpl implements RaidersGroupService {
    private static final Logger LOG = LoggerFactory.getLogger(RaidersGroupServiceImpl.class);

    @Resource
    private RaidersGroupModel raidersGroupModel;
    @Resource
    private RaidersHelp raidersHelp;
    @Resource
    private RaidersCorpModel raidersCorpModel;

    @Override
    public RaidersGroupListVO list(RaidersGroupListDTO raidersGroupListDTO) throws XbbException {
        try {
            //获得分组列表
            List<RaidersGroupEntity> groupList = raidersGroupModel.getValidListList(true);
            //获取分组下的攻略
            List<Long> groupIdList = new ArrayList<>();
            for (RaidersGroupEntity raidersGroup : groupList) {
                groupIdList.add(raidersGroup.getId());
            }
            String corpid = raidersGroupListDTO.getCorpid();
            UserVO loginUser = raidersGroupListDTO.getLoginUser();
            List<RaidersEntity> raidersList = raidersHelp.getRaidersList4Filter(corpid, groupIdList, loginUser);
            //计算每个攻略分组下的攻略数量
            Map<Long, Integer> raidersGroupIdRaidersCountMap = new HashMap<>(groupIdList.size());
            for (RaidersEntity raiders : raidersList) {
                Long groupId = raiders.getGroupId();
                Integer raidersCount = raidersGroupIdRaidersCountMap.get(groupId);
                if (raidersCount == null) {
                    raidersGroupIdRaidersCountMap.put(groupId, 1);
                } else {
                    raidersCount++;
                    raidersGroupIdRaidersCountMap.put(groupId, raidersCount);
                }
            }
            //如果攻略分组下的数量为零则删除该分组
            Iterator<RaidersGroupEntity> raidersGroupIterator = groupList.iterator();
            while (raidersGroupIterator.hasNext()) {
                RaidersGroupEntity raidersGroup = raidersGroupIterator.next();
                Long groupId = raidersGroup.getId();
                Integer raidersCount = raidersGroupIdRaidersCountMap.get(groupId);
                if (raidersCount == null || raidersCount == 0) {
                    raidersGroupIterator.remove();
                }
            }
            //获取推荐攻略和该公司绑定的个数
            Map<String, Object> raidersCorpMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
            raidersCorpMap.put("corpid", corpid);
            raidersCorpMap.put("del", DelEnum.NORMAL.getDel());
            raidersCorpMap.put("recommend", RaidersConstant.RECOMMEND);
            Integer recommendCount = raidersCorpModel.getEntitysCount(raidersCorpMap);
            List<RaidersNormalPojo> groupPojoList;
            if (recommendCount != null && recommendCount != 0) {
                groupPojoList = raidersGroupModel.handleGroupList2PojoList(groupList, 0);
            } else {
                groupPojoList = raidersGroupModel.handleGroupList2PojoList(groupList, null);
            }
            return new RaidersGroupListVO(groupPojoList);
        } catch (Exception e) {
            LOG.error("获得攻略列表报错", e);
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, SystemErrorCodeEnum.API_ERROR_100001.getMsg());
        }
    }
}
